package com.example.administrator.liveapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.bean.ArticleListBean;

import java.util.List;

/**
 * Created by Administrator on 2017/6/5 0005.
 */

public class ArticleRecyclerView1Adapter extends RecyclerView.Adapter<ArticleRecyclerView1Adapter.ViewHolder> implements View.OnClickListener {

    public Context context;
    public List<ArticleListBean.ArticleBean> article;
    public ArticleRecyclerView1Adapter(Context context, List<ArticleListBean.ArticleBean> article){
        this.context = context;
        this.article = article;
    }
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.article_recyclerview1_item,parent,false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ArticleRecyclerView1Adapter.ViewHolder holder, int position) {
        if(article.size()>0){
//            holder.line_ry1.setVisibility(View.VISIBLE);
            holder.article_text.setText(article.get(position).getArticletitle());
            holder.article_text2.setText(article.get(position).getArticledescribe());
            holder.article_comments.setText(article.get(position).getArticlecommcount());
            holder.article_watch_number.setText(article.get(position).getArticleviewcount());
            holder.article_Thumb_up_number.setText(article.get(position).getArticlebestcount());
            //将position保存在itemView的Tag中，以便点击时进行获取
            holder.itemView.setTag(position);
        }
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v,(int)v.getTag());
        }
    }

    @Override
    public int getItemCount() {
        if(article==null){
            return 0;
        }else{
            return article.size();
        }
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public TextView article_text;
        public TextView article_text2;
        public TextView article_comments;
        public TextView article_watch_number;
        public TextView article_Thumb_up_number;
//        public RelativeLayout line_ry1;
        public ViewHolder(View itemView) {
            super(itemView);
            article_text = (TextView) itemView.findViewById(R.id.article_text);
            article_text2 = (TextView) itemView.findViewById(R.id.article_text2);
            article_comments = (TextView) itemView.findViewById(R.id.article_comments);
            article_watch_number = (TextView) itemView.findViewById(R.id.article_watch_number);
            article_Thumb_up_number = (TextView) itemView.findViewById(R.id.article_Thumb_up_number);
//            line_ry1 = (RelativeLayout) itemView.findViewById(R.id.line_ry1);
        }
    }

    private ArticleRecyclerView1Adapter.OnItemClickListener mOnItemClickListener = null;

    public static interface OnItemClickListener {
        void onItemClick(View view , int position);
    }

    public void setOnItemClickListener(ArticleRecyclerView1Adapter.OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}
